﻿using System.Windows;
using IntelligentStorWpfApp.Controller;
using IntelligentStorWpfApp.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace IntelligentStorWpfApp.View
{
    /// <summary>
    /// OperationLog.xaml 的交互逻辑
    /// </summary>
    public partial class OperationLogWin : Window
    {
        public OperationLogWin()
        {
            InitializeComponent();
            WindowStartupLocation = WindowStartupLocation.CenterScreen;
        }
        private OperationLogController opcontrol;
        private List<OperationLog> listLogs;
        private void OperationLogWin_Loaded(object sender, RoutedEventArgs e)
        {
            opcontrol = new OperationLogController();
            listLogs = new List<OperationLog>();
            dgOplog.ItemsSource = null;
            listLogs = opcontrol.getAllLog();
            showProcessBar(listLogs);
            bindingData(App.Num, 1);
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            
            string userName = txtUserName.Text.Trim();
            string startTime = tpStartDat.Text;
            string  endTime = tpEndDat.Text;
            if(DateTime.Compare(Convert.ToDateTime( startTime),Convert.ToDateTime( endTime))>0)
            {
                MessageBox.Show("开始时间应早于结束时间!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            else
            {
                dgOplog.ItemsSource = null;
                listLogs = opcontrol.getLogByUserAndTime(userName, startTime, endTime);
                showProcessBar(listLogs);
                bindingData(App.Num, 1);
            }
        }

        private void showProcessBar(List<OperationLog> list)
        {
            this.probar1.Visibility = Visibility.Visible;
            int cout = list.Count;
            probar1.Maximum = cout;
            probar1.Value = 0;
            UpdateProgressBarDelegate updatePbDelegate = new UpdateProgressBarDelegate(probar1.SetValue);
            for (int i = 0; i < cout; i++)
            {
                Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { System.Windows.Controls.ProgressBar.ValueProperty, Convert.ToDouble(i + 1) });
                Thread.Sleep(10);
            }
        }

        //number表示每个页面显示的记录数    currentSize表示当前显示页数
        private void bindingData(int number, int currentSize)
        {
            
            List<OperationLog> infoList = new List<OperationLog>();
            infoList = listLogs;      //获取数据源
            int count = infoList.Count;          //获取记录总数
            int pageSize = 0;            //pageSize表示总页数
            if (count % number == 0)
            {
                pageSize = count / number;
            }
            else
            {
                pageSize = count / number + 1;
            }
            tbkTotal.Text = pageSize.ToString();

            tbkCurrentsize.Text = currentSize.ToString();
            infoList = infoList.Take(number * currentSize).Skip(number * (currentSize - 1)).ToList();   //刷选第currentSize页要显示的记录集

            dgOplog.ItemsSource = infoList;        //重新绑定dataGrid1
            this.probar1.Visibility = Visibility.Hidden;
        }

        private void btnGo_Click(object sender, RoutedEventArgs e)
        {
            int pageNum = int.Parse(tbxPageNum.Text);
            int total = int.Parse(tbkTotal.Text); //总页数
            if (pageNum >= 1 && pageNum <= total)
            {
                bindingData(App.Num, pageNum);     //调用分页方法
            }

        }

        private void btnUp_Click(object sender, RoutedEventArgs e)
        {
            int currentsize = int.Parse(tbkCurrentsize.Text); //获取当前页数
            if (currentsize > 1)
            {
                bindingData(App.Num, currentsize - 1);   //调用分页方法
            }

        }
        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            int total = int.Parse(tbkTotal.Text); //总页数
            int currentsize = int.Parse(tbkCurrentsize.Text); //当前页数
            if (currentsize < total)
            {
                bindingData(App.Num, currentsize + 1);   //调用分页方法
            }
        }

        private void dgOplog_LoadingRow(object sender, System.Windows.Controls.DataGridRowEventArgs e)
        {
            e.Row.Header = e.Row.GetIndex() + 1;
        }


        #region 进度条
        private delegate void UpdateProgressBarDelegate(System.Windows.DependencyProperty dp, Object value);

        private void beginImport(double max, double v)
        {
            probar1.Maximum = max;
            probar1.Value = v;
            UpdateProgressBarDelegate updatePbDelegate = new UpdateProgressBarDelegate(probar1.SetValue);
            for (int i = 0; i < listLogs.Count; i++)
            {
                Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { System.Windows.Controls.ProgressBar.ValueProperty, Convert.ToDouble(i + 1) });
            }
        }
        #endregion
    }
}
